<template>
  <!-- 新增企业 表单 -->
  <a-modal
    v-model:open="visible"
    :title="isEdit ? '编辑企业' : '新增企业'"
    class="addStation"
    destroyOnClose
    :width="520"
    @cancel="cancel"
    centered
  >
    <a-form
      :model="form"
      :rules="rules"
      layout="inline"
      :labelCol="labelCol"
      ref="formRef"
    >
      <a-form-item label="企业名称" name="projectName">
        <a-input
          v-model:value="form.projectName"
          class="formInput"
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="经度" name="longitude">
        <a-input-number
          v-model:value="form.longitude"
          class="formInput"
          placeholder="请输入"
          :controls="false"
        />
      </a-form-item>
      <a-form-item label="维度" name="latitude">
        <a-input-number
          v-model:value="form.latitude"
          class="formInput"
          placeholder="请输入"
          :controls="false"
        />
      </a-form-item>
      <a-form-item label="企业地址：" style="width: 461px" name="countyId">
        <div class="areaSelectContent">
          <a-select
            ref="select"
            v-model:value="form.provId"
            class="areaSelect"
            placeholder="省份"
            show-search
            optionFilterProp="label"
            @change="changeProv"
          >
            <a-select-option
              v-for="item in provList"
              :key="item.provId"
              :value="item.provId"
              :label="item.provName"
            >
              {{ item.provName }}
            </a-select-option>
          </a-select>
          <a-select
            ref="select"
            v-model:value="form.cityId"
            class="areaSelect"
            placeholder="城市"
            @change="changeCity"
          >
            <a-select-option
              v-for="item in cityList"
              :key="item.cityId"
              :value="item.cityId"
              :label="item.cityName"
            >
              {{ item.cityName }}
            </a-select-option>
          </a-select>
          <a-select
            ref="select"
            v-model:value="form.countyId"
            class="areaSelect"
            placeholder="区"
          >
            <a-select-option
              v-for="item in countyList"
              :key="item.countyId"
              :value="item.countyId"
              :label="item.countyName"
            >
              {{ item.countyName }}
            </a-select-option>
          </a-select>
        </div>
      </a-form-item>
      <a-form-item label="详细地址" name="address">
        <a-input
          v-model:value="form.address"
          class="formInput"
          placeholder="请输入"
        />
      </a-form-item>
    </a-form>
    <template #footer>
      <div class="footerBar">
        <a-button key="back" @click="cancel">取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确认</a-button
        >
      </div>
    </template>
  </a-modal>
</template>
<script setup>


const $api = inject("$api");
const props = defineProps({
  addCompanyVisible: {
    type: Boolean,
    default: false,
  },
  rowData: {
    type: Object,
  },
  isEdit: {
    type: Boolean,
    default: false,
  },
});
const loading = ref(false);
const visible = ref(props.addCompanyVisible);
const emits = defineEmits(["update:addCompanyVisible", "getCompageList"]);
watch(
  () => props.addCompanyVisible,
  (val) => {
    visible.value = val;
    if (val) {
      getBaseProvinceTree();
      if (props.isEdit) {
        form.value = {
          ...props.rowData,
        };
      }
    }
  }
);
const labelCol = { style: { width: "100px" } };
const formRef = ref(null);
const form = ref({
  projectName: null,
  longitude: null,
  latitude: null,
  provId: null,
  cityId: null,
  countyId: null,
  address: null,
});
const rules = ref({
  projectName: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  longitude: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  latitude: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],

  countyId: [
    {
      required: true,
      message: "请选择",
      trigger: "change",
    },
  ],
  address: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
});

const cancel = () => {
  emits("update:addCompanyVisible", false);
  form.value = {
    projectName: null,
    longitude: null,
    latitude: null,
    provId: null,
    cityId: null,
    countyId: null,
    address: null,
  };
  formRef.value.clearValidate();
};
const provList = ref([]);
const cityList = ref([]);
const countyList = ref([]);
const getBaseProvinceTree = async () => {
  const res = await $api.baseProvinceTree();
  provList.value = res.data;
};
const changeProv = async (id) => {
  form.value.cityId = null;
  form.value.countyId = null;
  cityList.value = [];
  countyList.value = [];
  const res = await $api.baseCityTree(id);
  cityList.value = res?.data;
};
const changeCity = async (id) => {
  form.value.countyId = null;
  countyList.value = [];
  const res = await $api.baseCountyTree(id);
  countyList.value = res?.data;
};
const handleSubmit = () => {
  formRef.value
    .validate()
    .then(async () => {
      const params = {
        ...form.value,
      };
      if (props.isEdit) {
        // 编辑带上id
        params.id = props.rowData.id;
      }
      loading.value = true;
      const res = await $api.companyAdd(params, props.isEdit ? "put" : "post");
      loading.value = false;
      if (res.code === 0) {
        message.success(props.isEdit ? "编辑成功" : "新增成功");
        cancel();
        emits("getCompageList");
      }
    })
    .catch((error) => {
      console.log("error", error);
    });
};
</script>

<style lang="less" scoped>
.form {
  padding: 0 40px;
  margin-top: 30px;
}
.formInput,
.formSelect {
  width: 350px;
}
.ant-form-item {
  margin-top: 20px;
}
.footerBar {
  text-align: center;
  margin-top: 30px;
}
.areaSelectContent {
  display: inline-block;
}
.areaSelectContent {
  display: inline-block;
}
.areaSelect {
  display: inline-block;
  width: 113px;
  margin-right: 5px;
  &:last-child {
    margin-right: 0;
  }
}
</style>